package org.spring.springboot.app.community.info.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.community.info.dao.CommunityInfoDao;
import org.spring.springboot.app.community.info.domain.po.CommunityInfoPO;
import org.spring.springboot.app.community.info.domain.vo.CommunityInfoInsertAndUpadteVO;
import org.spring.springboot.app.community.info.domain.vo.CommunityInfoSelectVO;
import org.spring.springboot.app.community.info.service.ICommunityInfoService;
import org.spring.springboot.app.community.structure.dao.CommunityStructureDao;
import org.spring.springboot.app.community.structure.domain.po.CommunityStructurePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CommunityInfoServiceImpl implements ICommunityInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommunityInfoServiceImpl.class);

    @Autowired
    private CommunityInfoDao communityInfoDao;
    @Autowired
    private CommunityStructureDao communityStructureDao;

    @Override
    public List<CommunityInfoSelectVO> findByParent(String parentId) {
        return null;
    }

    @Override
    public CommunityInfoSelectVO find(String id) {
        return communityInfoDao.findById(id);
    }

    @Override
    public List<CommunityInfoSelectVO> findByOfficeId(String officeId) {
        return communityInfoDao.findByOfficeId(officeId);
    }

    @Override
    public List<CommunityInfoSelectVO> findPage(int pageNum, int pageSize) {
        return null;
    }

    @Override
    public int save(CommunityInfoInsertAndUpadteVO vo) {
        //添加组织机构表
        CommunityStructurePO structure=new CommunityStructurePO();
        structure.preInsert();
        structure.setAreaId(vo.getAreaId());
        structure.setOfficeId(vo.getOfficeId());
        structure.setType(Byte.valueOf("1"));//小区
        structure.setName(vo.getName());
        communityStructureDao.insertSelective(structure);
        //添加小区构表
        CommunityInfoPO record=new CommunityInfoPO();
        record.preInsert();
        record.setId(structure.getId());
        record.setAddress(vo.getAddress());
        record.setIndexImage("");
        record.setRelateId(vo.getRelateId());
        record.setLongitude(vo.getLongitude());
        record.setLatitude(vo.getLatitude());
        record.setPhone(vo.getPhone());
        record.setIsCostRestrict(vo.getIsCostRestrict());
        record.setCostRestrictDay(vo.getCostRestrictDay());
        record.setHouseOtherCost(Double.valueOf("0"));
        return communityInfoDao.insertSelective(record);
    }

    @Override
    public int update(CommunityInfoInsertAndUpadteVO vo) {
        //添加组织机构表
        CommunityStructurePO structure=new CommunityStructurePO();
        structure.setId(vo.getId());
        structure.setAreaId(vo.getAreaId());
        structure.setOfficeId(vo.getOfficeId());
        structure.setType(Byte.valueOf("1"));//小区
        structure.setName(vo.getName());
        communityStructureDao.updateByPrimaryKeySelective(structure);
        //添加小区构表
        CommunityInfoPO record=new CommunityInfoPO();
        record.setId(vo.getId());
        record.setId(structure.getId());
        record.setAddress(vo.getAddress());
        record.setRelateId(vo.getRelateId());
        record.setLongitude(vo.getLongitude());
        record.setLatitude(vo.getLatitude());
        record.setPhone(vo.getPhone());
        record.setIsCostRestrict(vo.getIsCostRestrict());
        record.setCostRestrictDay(vo.getCostRestrictDay());
        //修改小区及以下的区域
        communityInfoDao.updateAreaIdById(vo.getId(),vo.getAreaId());
        return communityInfoDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(String id) {
        CommunityStructurePO structure=new CommunityStructurePO();
        structure.setId(id);
        structure.setDelFlag("1");
        communityStructureDao.updateByPrimaryKeySelective(structure);
        CommunityInfoPO record=new CommunityInfoPO();
        record.setId(id);
        record.setDelFlag("1");
        return communityInfoDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deletes(String ids) {
        return 0;
    }

}
